package com.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 客户机的初始化、关闭
 */
public class ClientInitClose {
    // 定义Socket对象
    private Socket socket;
    // 定义输入输出流
    private ObjectInputStream objectInputStream;
    private ObjectOutputStream objectOutputStream;

    /**
     * 获取输出流
     * @return
     */
    public ObjectInputStream getObjectInputStream() {
        return objectInputStream;
    }

    /**
     * 获取输入流
     * @return
     */
    public ObjectOutputStream getObjectOutputStream() {
        return objectOutputStream;
    }

    /**
     * 客户机的初始化
     */
    public void initClient() throws IOException {
        // 客户端与服务器连接
        socket = new Socket("localhost",9001);
        objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        objectInputStream = new ObjectInputStream(socket.getInputStream());
        System.out.println("连接服务器成功！");

    }
    /**
     * 客户机关闭
     */
    public void closeClient() throws IOException {
        objectOutputStream.close();
        objectInputStream.close();
        socket.close();
        System.out.println("客户机关闭成功！");
    }
}
